package name.vaccari.matteo.tai.phonedirectory.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TcpClient {

	private final int port;
	private final String host;
	private Socket socket;
	private BufferedReader reader;
	private PrintWriter writer;

	public TcpClient(String host, int port) {
		this.host = host;
		this.port = port;
	}

	public void connect() throws IOException {
		socket = new Socket(host, port);
		reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		writer = new PrintWriter(socket.getOutputStream());
	}

	public void close() {
		try {
			socket.close();
		} catch (IOException ignored) {
		}
	}

	public void writeLine(String line) {
		writer.println(line);
		writer.flush();
	}

	public String readLine() throws IOException {
		return reader.readLine();
	}

}
